import { createRouter, createWebHistory } from 'vue-router'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    //
    { path: "/", redirect: "/login"},
    { path: '/login', name: "login", component: () => import('@/views/Login.vue')},
    { path: '/realtime', name: "realtime", component: () => import('@/views/RealTime.vue'), meta:{ requiresAuth: true }},
    { path: '/greenHouseInformation', name: "greenHouseInformation", component: () => import('@/views/GreenHouseInformation.vue'),meta:{ requiresAuth: true }}
  ],
})

router.beforeEach((to, from, next) => {
  if (to.meta.requiresAuth && !localStorage.getItem('token')) {
    // 如果目标路由需要登录且没有token，则重定向到登录页
    next('/login');
  } else {
    next();
  }
});
export default router
